{% extends "base/micro.html" %}

{% block container %}
    {% set user = g.api.user_get_one_profile(g.username).data %}
    <form role="form" id="UpdateUserFormId">
        <div class="form-group">
            <label for="cname">请输入你的别名：</label>
            <input type="text" class="form-control" id="cname" name="cname" value="{{ user.cname or '' }}" placeholder="请在这里写上您的昵称吧" />
        </div>
        <div class="form-group">
            <label for="url">请输入你的网址：</label>
            <input type="text" class="form-control" id="url" name="url" value="{{ user.url or '' }}" placeholder="请在这里写上您的个人网址吧" />
        </div>
        <div class="form-group">
            <label for="motto">请输入你的格言：</label>
            <input type="text" class="form-control" id="motto" name="motto" value="{{ user.motto or '' }}" placeholder="请在这里写上您的座右铭吧" />
        </div>
        <div class="form-group">
            <label for="github">请输入你的GitHub：</label>
            <input type="url" class="form-control" id="github" name="github" value="{{ user.github or '' }}" placeholder="请在这里写上您的GitHub地址吧" />
        </div>
        <div class="form-group">
            <label for="weibo">请输入你的Weibo：</label>
            <input type="url" class="form-control" id="weibo" name="weibo" value="{{ user.weibo or '' }}" placeholder="请在这里写上您的Weibo地址吧" />
        </div>
        <div class="form-group">
            <label for="email">请输入你的邮箱：</label>
            <input type="email" class="form-control" id="email" name="email" value="{{ user.email or '' }}" placeholder="请在这里写上您的公开邮箱吧" />
        </div>
        <div class="form-group">
            <label for="email">请选择你的性别：</label>
            {%- if user.gender == '男' %}
                <div class="radio-inline">
                    <label><input type="radio" name="gender" id="gender" value="男" checked>男</label>
                </div>
                <div class="radio-inline">
                    <label><input type="radio" name="gender" id="gender" value="女">女</label>
                </div>
            {% else %}
                <div class="radio-inline">
                    <label><input type="radio" name="gender" id="gender" value="男">男</label>
                </div>
                <div class="radio-inline">
                    <label><input type="radio" name="gender" id="gender" value="女" checked>女</label>
                </div>
            {% endif -%}
        </div>
        <input type="hidden" name="username" value="{{ g.username }}">
        <input type="reset" value="内容重置" class="btn btn-success" style="float: left;" />
        <input type="button" onclick=putForm() value="更新资料" class="btn btn-danger" />
    </form>
{% endblock %}

{% block script %}
<script type="text/javascript">
    // 更新用户资料
    function putForm() {
        $.ajax({
            url: '{{ url_for("api.user", ChangeType="profile") }}',
            method: 'PUT',
            datatype: 'json',
            data: $('#UpdateUserFormId').serialize(),
            success: function(res) {
                console.log(res);
                if (res.success == true){
                    layer.msg("更新用户资料成功！", {icon: 1, time: 3*1000});
                    parent.location.reload();
                } else {
                    layer.alert("更新用户资料失败，请重试提交或报告错误！\n错误原因是：" + res.msg, {icon: 2, time: 15*1000});
                }
            },
        });
    };
</script>
{% endblock %}